<template>
  <div class="container">
    <div class="dist-area">
      <v-distpicker
        hide-area
        :province="searchParams.province"
        :city="searchParams.city"
        @province="onChangeProvince"
        @city="onChangeCity"
      />
    </div>
    <el-input v-model="searchParams.equipId" style="width: 200px" placeholder="键入设备id" />
    <el-input v-model="searchParams.equipName" style="width: 200px" placeholder="键入设备用户名" />
    <el-button type="primary" icon="el-icon-search" @click="doSearch">检索</el-button>

    <el-table v-loading="tableCtl.loading" border :data="pageList.equipList">
      <el-table-column label="id" align="center" width="80" prop="equip.id" />
      <el-table-column label="注册时间">
        <template slot-scope="{row}">
          <span>
            {{ row.equip.gmtModified | dateFormat }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="设备ID" prop="equip.equipId" />
      <el-table-column label="设备位置">
        <template slot-scope="{row}">
          <span>
            {{ getLocation(row.equip.equipUserLocation).province }}
          </span>
          <div>
            {{ getLocation(row.equip.equipUserLocation).city }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="设备用户名" prop="equip.equipUserName" />
      <el-table-column label="设备用户身份证" prop="equip.equipUserIdentify" />
      <el-table-column label="操作">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="diaCtr.show = true; diaCtr.data = row.userList">关联用户信息</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :page-sizes="[10, 15, 20, 25]"
      :page-size="pagination.size"
      :current-page.sync="pagination.index"
      :total="pagination.total"
      layout="sizes, prev, pager, next"
      @size-change="handlerPageSizeChange"
      @current-change="handlerPageIndexChange"
    />
    <el-dialog :visible.sync="diaCtr.show" title="签约详情" width="70%">
      <simTable :data="diaCtr.data" :header="diaCtr.header" :title="diaCtr.title">
        <template>
          <el-table-column label="签约状态">
            <template slot-scope="{row}">
              <template v-if="row.sign.asUserType==='DOCTOR'">
                <el-tag v-if="row.sign.patientConfirm ==='agree'" type="success">患者已确认</el-tag>
                <el-tag v-else type="warning">患者待确认</el-tag>
                <el-tag v-if="row.sign.doctorConfirm ==='agree'" type="success">医生已确认</el-tag>
                <el-tag v-else type="warning">医生待确认</el-tag>
              </template>
              <template v-else>
                <el-tag type="success">状态正常</el-tag>
              </template>
            </template>
          </el-table-column>
          <el-table-column label="签约类型">
            <template slot-scope="{row}">
              <el-tag>
                {{ row.sign.asUserType === 'DOCTOR'? '医生':'用户' }}
              </el-tag>
            </template>
          </el-table-column>
        </template>
      </simTable>
    </el-dialog>
  </div>
</template>

<script>
import { req } from '@/api/equipSign'
import { getLocation } from '@/utils/loc'
import SimTable from '@/views/table'
export default {
  components: { SimTable },
  data() {
    return {
      tableCtl: {
        loading: false
      },
      pageList: {
        equipList: null
      },
      searchParams: {
        equipId: null,
        equipName: null,
        equipIdentify: null,
        modiStart: null,
        modiEnd: null,
        province: null,
        city: null,
        pageSize: 10,
        pageIndex: 1
      },
      diaCtr: {
        show: false,
        data: null,
        header: [
          {
            label: 'id',
            prop: 'user.id'
          },
          {
            label: '用户名',
            prop: 'user.userName'
          },
          {
            label: '用户电话',
            prop: 'user.userPhone'
          },
          {
            label: '用户自我描述',
            prop: 'user.userDiscription'
          }
        ],
        title: '用户详情'
      },
      pagination: {
        size: 10,
        index: 1,
        total: 0
      }
    }
  },
  created() {
    this.upLoading()
    req().then((response) => {
      this.pageList.equipList = response.object.obj
      this.pagination.total = response.object.pageTotal
      this.downLoading()
    })
  },
  methods: {
    upLoading() {
      this.tableCtl.loading = true
    },
    downLoading() {
      this.tableCtl.loading = false
    },
    doSearch() {
      this.searchParams.pageIndex = 1
      this.pagination.index = 1
      if (!this.searchParams.equipId || this.searchParams.equipId.size <= 0) {
        this.searchParams.equipId = null
      }
      if (!this.searchParams.equipName || this.searchParams.equipName.size <= 0) {
        this.searchParams.equipName = null
      }
      this.handlerSearch()
    },
    handlerSearch() {
      this.upLoading()
      req(this.searchParams).then(
        response => {
          this.pageList.equipList = response.object.obj
          this.pagination.total = response.object.pageTotal
          this.downLoading()
        }
      )
    },
    onChangeProvince(data) {
      this.searchParams.province = data.value === '省' ? null : data.value
    },
    onChangeCity(data) {
      this.searchParams.city = data.value === '市' ? null : data.value
    },
    handlerPageSizeChange(e) {
      this.pagination.size = e
      this.pagination.index = 1
      this.searchParams.pageSize = e
      this.searchParams.pageIndex = 1
      this.pagination.total = 0
      this.handlerSearch()
    },
    handlerPageIndexChange(e) {
      this.pagination.index = e
      this.searchParams.pageIndex = e
      this.handlerSearch()
    },
    getLocation(data) {
      return getLocation(data)
    }
  }
}
</script>

<style lang="scss">
@import '@/styles/custom.scss';
.container {
  padding: 20px;
}
</style>
